<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <#include "/back/common.html">
    <!--<link rel="stylesheet" href="/back/layui/css/layui.css">-->
    <!--<link rel="stylesheet" href="/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">-->
    <!--<script src="/back/js/jquery.min.js"></script>
    <script src="/back/layui/layui.js"></script>-->
    <!--<script type="text/javascript" src="/zTree/js/jquery.ztree.all.js"></script>-->
    <!--<script type="text/javascript" src="/js/myTree.js"></script>-->
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>Banner图信息</legend>
</fieldset>

<form class="layui-form" id="userForm">
    <input type="hidden" id="pk" name="pk" value="${mv.pk}">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"><font color="red">※</font>名称</label>
            <div class="layui-input-inline">
                <input type="text" name="barName"  id="barName" value="${mv.bar_name}"
                       lay-verify="notNull" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-inline">
                <input type="text" name="barDesc" id="barDesc" value="${mv.bar_desc}" lay-ignore="true" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上传图片</label>
        <div class="layui-input-block" id="uploadImg" style="padding-right: 5px; margin-left: 15px;">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="uploadButton">图片上传</button>
            </div>
        </div>
    </div>
    <input type="hidden" name="barImage" id="barImage" value="${mv.bar_image}"><!--图片路径隐藏域-->

    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit="" lay-filter="demo1">立即提交</button>
            <!--<button class="layui-btn layui-btn-primary layui-btn-radius" type="reset">重置</button>-->
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'layedit', 'laydate','upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , upload = layui.upload;
        form.render();

        //pk不为空，编辑页的时候
        var pk = $("#pk").val();
        if(pk!=""&&pk!=null&&pk!=undefined){
            //显示图片
            var opt= "<img src=\"//${mv.bar_image}\" style=\"width:200px;height:160px;\" id=\"uploadButton\">";
            $("#uploadImg").html(opt);
        }

        //单图片上传
        upload.render({
            elem: '#uploadButton'
            ,url: '/aliOSS/fileUpload'
            ,data: {type: '轮播图片'}
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                /*obj.preview(function(index, file, result){
                    $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="width: 100px;height: 80px;">')
                });
                console.log(obj)*/
            }
            ,done: function(res){
                if(res.success){
                    var opt= "<img src=\"//"+res.data[0].path+"\" style=\"width:200px;height:160px;\" id=\"uploadButton\">";
                    $("#uploadImg").html(opt);
                    $("#barImage").val(res.data[0].path);
                }
                layer.msg(res.message)
            }
            ,error: function(){
                //演示失败状态，并实现重传
                layer.msg("未知异常");
            }
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            layui.use(['jquery'], function () {
                var $ = jQuery = layui.$;
                var postUrl = "/banner/saveOrUpdate";
                $.ajax({
                    url: postUrl,
                    type: 'post',
                    data: $("#userForm").serialize(),
                    success: function (resp) {
                        layer.msg(resp.message);
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        parent.initTable();//刷新表格数据
                    },
                    error: function (resp) {
                        layer.msg(resp.message);
                    }
                });
            });
            return false;
        });
    });
</script>

</body>
</html>